package com.stox.ui.javafx.charting.model.plot;

import java.util.ArrayList;
import java.util.List;

import com.stox.model.AbstractModel;
import com.stox.model.Bar;
import com.stox.ui.javafx.charting.model.uiunit.BarPrimaryUiUnit;
import com.stox.ui.javafx.charting.strategy.DefaultBarPrimaryUiUnitColorStrategy;
import com.stox.ui.javafx.charting.widget.PrimaryChart;

public class BarPrimaryPlot extends AbstractPrimaryPlot<	Bar, 
																				PrimaryChart<BarPrimaryPlot>, 
																				BarPrimaryUiUnit, 
																				BarPrimaryPlot> {
	public BarPrimaryPlot(AbstractModel model) {
		super(model);
		setColorStrategy(DefaultBarPrimaryUiUnitColorStrategy.instance);
	}
	
	public void setVolumeData() {
		VolumePlot volumePlot = getChart().getChartingView().getVolumePlot(); 

		if(null == volumePlot || !volumePlot.getModel().equals(getChart().getFrontPlot().getModel())) {
			if(null != volumePlot) volumePlot.removeFromChart();
			volumePlot = new VolumePlot(getChart().getChartingView(), getChart().getFrontPlot().getModel());
			getChart().getChartingView().setVolumePlot(volumePlot);
		}
		
		List<Number> data = new ArrayList<>(getData().size());
		for(Bar bar : getData()) data.add(bar.getVolume());
		volumePlot.setData(data);
	}
	
	@Override
	protected void createUiUnits() {
		for(int i = 0; i < getData().size(); i++)			
			getUiUnits().add(new BarPrimaryUiUnit(getData().get(i),this,i));
	}
	
	
	
	@Override
	public void update() {
		super.update();
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			Bar bar = getData().get(index);
			if (highest < bar.getHigh())
				highest = bar.getHigh();
			if (lowest > bar.getLow())
				lowest = bar.getLow();
			volumeSum += bar.getVolume();
		}
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).update();
		}
	}
	
	@Override
	public void paint() {
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).paint();
		}
	}

}
